#!/usr/bin/env python
# encoding: utf-8
from warnings import filterwarnings
import datetime
import DAL
import json
import os
import decimal

os.environ['NLS_LANG'] = 'SIMPLIFIED CHINESE_CHINA.UTF8'

class FormatEncoder(json.JSONEncoder):
    def default(self,obj):
        if isinstance(obj, datetime.datetime):
            return obj.strftime('%Y-%m-%d %H:%M:%S')
        elif isinstance(obj,decimal.Decimal):
            return float(obj)
        super(FormatEncoder,self).default(obj)


def show_china(s):
	print json.dumps(s, indent = 4, cls=FormatEncoder, ensure_ascii = False)

#----------------------------------------------------------------------------------------------------
def msql_select(sql):
	con_msql = DAL.Mysql('127.0.0.1','root','Mypassword@2qq','af', 33966)
	if con_msql:
		print "connected"
		rowcount, result = con_msql.execute(sql)
		print result
	con_msql.close()
	return result

def msql_executemany(sql,data):
	con_msql = DAL.Mysql('127.0.0.1','root','Mypassword@2qq','af', 33966)
	if con_msql:
		print "connected"
		effect_row = con_msql.executemany(sql,data)
		print effect_row
		print '-'*80
        #con_msql.commit()
	con_msql.close()
	return effect_row


# def sqls_select(sql,connect_):
# 	con_sqls = DAL.sqlserver(connect_[0],connect_[1],connect_[2])
# 	if con_sqls:
# 		print "connected"
# 		rowcount, result, title= con_sqls.execute(sql)
# 		show_china(result)
# 		print len(result)
#         title = [i[0] for i in title]
#         print title
# 	con_sqls.close()
# 	return result,title


# def oracl_sql(sql,connect_):
#     con_orac = DAL.Oracle(connect_[0],connect_[1],connect_[2],1521,connect_[3])
#     if con_orac:
#         print "connected"
#         rowcount, result, title = con_orac.execute(sql)
#         title = [i[0].lower() for i in title]
#         title = [tran_dict[i] if i in tran_dict.keys() else i for i in title]
#         show_china(result)
#         #print result
#         print title
#     con_orac.close()
#     return result,title

 
if __name__ == '__main__':
    sql = "select * from emr.v_user_views"
    oracl_sql(sql,connect_['emr'])

